package com.net;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import android.content.pm.ResolveInfo;
import android.os.Handler;
import android.os.Message;
import android.os.Process;

import com.constant.Constant;
import com.server.data.ActivateData;
import com.server.data.BaseData;
import com.server.data.BuyResourceData;
import com.server.data.ClientData;
import com.server.data.DownloadData;
import com.server.data.DownloadLogData;
import com.server.data.DownloadPackageData;
import com.server.data.PayLogData;
import com.server.data.RegistData;
import com.server.data.ResourceData;
import com.server.data.UpdatePwdData;

/**
 * 数据加载类接口
 * 
 * @author lenovo
 * 
 */
public final class LoadData {
	private final static int MESSAGE_OK = 0x1;
	private final static int MESSAGE_ERROR = 0x2;
	private static ThreadPoolExecutor mThreadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(3);

	public static void loadData(final String control_name, final RequestParameter parameter, final RequestListener listener) {

		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MESSAGE_OK:
					listener.onComplete(msg.obj);
					break;
				case MESSAGE_ERROR:
					listener.onError((String) msg.obj);
					break;
				}

			}
		};

		mThreadPool.submit(new Runnable() {

			public void run() {
				// 设置为后台线程，当主线程退出时，所有的后台线程都会退出
				Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
				try {
					BaseData data = loadObject(control_name);
					Message msg = null;
					if (data == null) {
						// 请求关键字未匹配
						msg = Message.obtain(handler, MESSAGE_ERROR, "请求关键字错误");
					} else {
						// 解析数据
						data.startParse(parameter);
						msg = Message.obtain(handler, MESSAGE_OK, data);
					}
					msg.sendToTarget();

				} catch (Exception e) {
					Message msg = Message.obtain(handler, MESSAGE_ERROR,
							"错误消息：" + e.getMessage());
					msg.sendToTarget();
					e.printStackTrace();
				}
			}
		});

	}

	private static BaseData loadObject(final String control_name) {
		BaseData data = null;
		if(control_name.equals(Constant.REGIST_DATA))
		{  
			data = new RegistData();
		}else if(control_name.equals(Constant.ACTIVATE_DATA))
		{ 
			data = new ActivateData();
		}
		else if(control_name.equals(Constant.BUY_RESOURCE_DATA))
		{
			data = new BuyResourceData();
		}
		else if(control_name.equals(Constant.UPDATE_PWD_DATA))
		{
			data = new UpdatePwdData();
		}
		else if(control_name.equals(Constant.CLIENT_DATA)) 
		{
			data = new ClientData();
		}
		else if(control_name.equals(Constant.PAY_LOG_DATA)) 
		{
			data = new PayLogData();
		}
		else if(control_name.equals(Constant.RESOURCE_DATA))
		{
			data = new ResourceData();
		}
		else if(control_name.equals(Constant.DOWNLOAD_DATA))
		{
			data = new DownloadData();
		}
		else if(control_name.equals(Constant.DOWNLOAD_PACKAGE_DATA)) 
		{
			data = new DownloadPackageData();
		}
		else if(control_name.equals(Constant.DOWNLOAD_LOGS_DATA)) 
		{
			data = new DownloadLogData();
		}
		else
		{
			return null;
		}
		
		return data;
	}
}
